package com.yqy.wordvideodemo.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.tencent.smtt.sdk.QbSdk;
import com.yqy.wordvideodemo.R;
import com.yqy.wordvideodemo.permission.IXPermissionCallback;
import com.yqy.wordvideodemo.permission.XPermission;
import com.yqy.wordvideodemo.permission.bean.AppPermission;

import java.util.List;

public class SplashActivity extends AppCompatActivity {

    private boolean isShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        checkPer();
    }

    private void checkPer() {
        String[] perms = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE};

        XPermission.getInstance().with(this)
                .setDeniedNumberMax(2)
                .checkPermission(perms, new IXPermissionCallback() {
                    @Override
                    public void onAcceptedAll() {
                        onAcceptedAllAfter();
                    }

                    @Override
                    public boolean onActivityResult(String[] permissions, int requestCode, int resultCode, Intent data) {
                        //修复用户拒绝所有权限 无限弹出对话框问题
                        if (isShow) {
                            isShow = false;
                            checkPer();
                        }
                        if (!isShow) {
                            isShow = true;
                        }
                        return true;
                    }

                    @Override
                    public boolean onDeniedAndReject(List<AppPermission> permissionsDenied, List<AppPermission> permissionsReject) {
                        isShow = false;
                        return false;
                    }
                });

    }


    private void onAcceptedAllAfter() {
        //初始化X5内核 这个初始化有说放在权限请求后初始，有说放在application 我测试这俩种都好用
        initX5();
    }

    private void initX5() {
        QbSdk.setDownloadWithoutWifi(true);
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                //x5内核初始化完成回调接口，此接口回调并表示已经加载起来了x5，有可能特殊情况下x5内核加载失败，切换到系统内核。
                Log.e("@@","加载内核是否onCoreInitFinished成功:");
            }

            @Override
            public void onViewInitFinished(boolean b) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.e("@@","加载内核是否成功:"+b);
            }
        });
    }

    public void btn(View view) {
        startActivity(new Intent(this, MainActivity.class));
    }

    public void btn2(View view) {
        String source1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
        VideoShowActivity.Companion.startVideoActivity(this,source1);
    }
}
